#include "Phoenix.h"
#include "NCManager.h"
#include "Domain.h"
#include "Statistics.h"
#include <queue>
#include <fstream>
#include <iostream>
#include <map>
#define STATS

using namespace std;

Phoenix::Phoenix()
{
}

Phoenix::~Phoenix()
{
}

void Phoenix::backFromFire()
{
	/**
	 * Recovering Queues
	 */
	cout << "Started recovering Queues" << endl;
	ifstream F1;
	F1.open("backup/Queue1.bup", ios_base::in);
	if(F1.is_open())
	{
		queue<string>* Q1 = NCManager::mainShip->getVisitQueue1();
		while(!F1.eof() && Q1->size()<700000)
		{
			string url="";
			getline(F1,url);
			//cout << url << endl;
			Q1->push(url);
		}
		F1.close();
	}
	
	ifstream F2;
	F2.open("backup/Queue2.bup", ios_base::in);
	if(F2.is_open())
	{
		queue<string>* Q2 = NCManager::mainShip->getVisitQueue2();
		while(!F2.eof() && Q2->size()<250000)
		{
			string url="";
			getline(F2,url);
			//cout << url << endl;
			Q2->push(url);
		}
		F2.close();
	}
	
	ifstream F3;
	F3.open("backup/Queue3.bup", ios_base::in);
	if(F3.is_open())
	{
		queue<string>* Q3 = NCManager::mainShip->getVisitQueue3();
		while(!F3.eof() && Q3->size()<100000)
		{
			string url="";
			getline(F3,url);
			//cout << url << endl;
			Q3->push(url);
		}
		F3.close();
	}
	
	ifstream F4;
	F4.open("backup/Queue4.bup", ios_base::in);
	if(F4.is_open())
	{
		queue<string>* Q4 = NCManager::mainShip->getVisitQueue4();
		while(!F4.eof() && Q4->size()<70000)
		{
			string url="";
			getline(F4,url);
			//cout << url << endl;
			Q4->push(url);
		}
		F4.close();
	}
	
	cout << "finished recovering queues" << endl;
	/**
	 * Finished rescuing Queues
	 */
	
	/**
	 * Starting to recover the Domains
	 */
	
		ifstream FD;
		FD.open("backup/Domains.bup", ios_base::in);
		if(FD.is_open())
		{
			int totalSize=0;
			while(!FD.eof())
			{
				string domainStr;				
				getline(FD,domainStr);
				cout << " Li o Dominio: " << domainStr << endl;
				//cout << domainStr << endl;
				//cout << domainStr << endl;
				totalSize+= (2*domainStr.size());
				Domain* D = new Domain();
				D->_domain=domainStr;
				string fetchRobots;
				getline(FD,fetchRobots);
				if(fetchRobots == "0")
					D->_fetchRobots=false;
				else
					D->_fetchRobots=true;
				
				string hasRobots;
				getline(FD,hasRobots);
				if(hasRobots=="0")
					D->_hasRobots=false;
				else
					D->_hasRobots=true;
				
				int whenAvailable=0;
				FD >> whenAvailable;
				D->_whenAvailable = static_cast<time_t>(whenAvailable);
				totalSize+= (2+ sizeof(time_t));
				string next;
				getline(FD, next);
				//while(next!="#" && !FD.eof())
				//{
				//	NCManager::mainShip->addToDownloadSetMD5(next);
				//	getline(FD, next);
				//} 
				
				//cout << "Adicionei o Dominio: " << domainStr << endl;				
				NCManager::mainShip->addDomain(D); 
			}		
			FD.close();	
		cout << "TAMANHO TOTAL: " << totalSize << endl;
		}
	
		cout << "finished recovering domains" << endl;
		/**
		 * Domains have been recovered xD
		 */
		
		ifstream FDD;
		FDD.open("backup/Downloaded.bup", ios_base::in);
		if(FDD.is_open())
		{				
		while(!FDD.eof())
		{
			string s;
			getline(FDD,s);
			NCManager::mainShip->addToDownloadSetMD5(s);
		}
		FDD.close();
		}
		/**
		 * Yet recovering the most important stats;
		 */
		
		#ifdef STATS
		
		ifstream FS;
		FS.open("backup/statsReport.log", ios_base::in);
		if(FS.is_open())
		{
			string aux;
			int sites=0;
			int robots=0;
			getline(FS,aux);
			cout << aux << endl;
			getline(FS,aux);
			cout << aux << endl;
			FS >> aux;
			FS >> aux;
			cout << aux << endl;
			FS >> sites;
			cout << sites << endl;
			FS >> aux;
			FS >> aux;
			cout << aux << endl;
			FS >> robots;
			cout << robots << endl;
			NCManager::visitedSites=sites;
			NCManager::fetchedRobots=robots;
			FS.close();
		}	

		#endif
		
		cout << "finished recovering stats" << endl;
			
	}
	
